﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Text;

public partial class ContactUs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private void clearAll()
    {
        txtFirstName.Text = "";
        txtLastName.Text = "";
        txtEmail.Text = "";
        txtMobile.Text = "";
        txtSubject.Text = "";
        txtMessage.Text = "";
        lablMess.Text = "";
        txtFirstName.Focus();
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        clearAll();
    }

    public static bool SendEmail(string pGmailEmail, string pGmailPassword, string pTo, string pSubject, string pBody, System.Web.Mail.MailFormat pFormat, string pAttachmentPath)
    {
        try
        {
            System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
            myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/smtpserver",
                              "smtp.gmail.com");
            myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/smtpserverport",
                              "465");
            myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/sendusing",
                              "2");

            myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
            //Use 0 for anonymous
            myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/sendusername",
                pGmailEmail);
            myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/sendpassword",
                 pGmailPassword);
            myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/smtpusessl",
                 "true");
            myMail.From = pGmailEmail;
            myMail.To = pTo;
            myMail.Subject = pSubject;
            myMail.BodyFormat = pFormat;
            myMail.Body = pBody;


            System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465";
            System.Web.Mail.SmtpMail.Send(myMail);
            return true;
        }
        catch (Exception ex)
        {
            throw;
        }
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        GenericDAL.setContactInformation(txtFirstName.Text, txtLastName.Text, txtEmail.Text, txtMobile.Text, txtSubject.Text, txtMessage.Text);

        StringBuilder sbMail = new StringBuilder();
        sbMail.Append("<table width='70%'><tr><td>First name:<b> " + txtFirstName.Text + "</b></td></tr>");
        sbMail.Append("<tr><td>Last Name:<b> " + txtLastName.Text + "</b></td></tr><tr><td></td></tr>");
        sbMail.Append("<tr><td>Email Id:<b> " + txtEmail.Text + "</b></td></tr></table>");
        sbMail.Append("<tr><td>Mobile No:<b> " + txtMobile.Text + "</b></td></tr></table>");
        sbMail.Append("<tr><td>Subject:<b> " + txtSubject.Text + "</b></td></tr></table>");
        sbMail.Append("<tr><td>Message:<b> " + txtMessage.Text + "</b></td></tr></table>");
        
        ConfigurationManager.AppSettings["ContactEmailTo"] = "contact@skilledres.com";
        if (SendEmail(ConfigurationManager.AppSettings["ContactEmailFrom"], ConfigurationManager.AppSettings["ContactEmailPassword"], ConfigurationManager.AppSettings["ContactEmailTo"],"\""+  txtFirstName.Text+" "+txtLastName.Text + "\" Contacted Through Website", sbMail.ToString(), System.Web.Mail.MailFormat.Html, "") == true)
        {

        }

        clearAll();
        lablMess.Text = "Your request has been sent, we will get in touch with you shortly.";
    }
}